package com.xgit.iov.admin.controller;


import com.xgit.iov.admin.properties.IndexProperties;
import com.xgit.iov.admin.service.ESIndexService;
import com.xgit.iov.common.core.entity.ESIndex;
import com.xgit.iov.common.core.util.R;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Map;

/**
 * 索引管理
 */
@Slf4j
@RestController
@Api(tags = "索引管理模块")
@RequestMapping("/es")
public class ESIndexController {
    @Autowired
    private ESIndexService indexService;

    @Autowired
    private IndexProperties indexProperties;

    @PostMapping("/index")
    public R createIndex(@RequestBody ESIndex esIndex) throws IOException {
        if (esIndex.getNumberOfShards() == null) {
			esIndex.setNumberOfShards(1);
        }
        if (esIndex.getNumberOfReplicas() == null) {
			esIndex.setNumberOfReplicas(0);
        }
        indexService.create(esIndex);
        return R.ok("操作成功");
    }

    /**
     * 索引列表
     */
    @GetMapping("/indices")
    public R list(@RequestParam(required = false) String queryStr) throws IOException {
    	return R.ok(indexService.list(queryStr, indexProperties.getShow()));
    }

    /**
     * 索引明细
     */
    @GetMapping("/index/{indexName}")
    public R showIndex(@PathVariable String indexName) throws IOException {
        Map<String, Object> result = indexService.show(indexName);
        return R.ok(result);
    }

    /**
     * 删除索引
     */
    @DeleteMapping("/index/{indexName}")
    public R deleteIndex(@PathVariable String indexName) throws IOException {
        indexService.delete(indexName);
        return R.ok("操作成功");
    }
}
